Email Using Sendgrid SDK

  • STEP 1 : Coding

    1. ,env

    
                     SENDGRID_API_KEY=''
                    

    2. composer.json

    
                    {
                      "require": {
                        "sendgrid/sendgrid": "~7"
                      }
                    }
                    

    update composer

    
                    composer update
                   

    3. call the mail class in the controller

    
    use SendGrid;
    use SendGrid\Mail\Mail;
    
    
    
    $email = new \SendGrid\Mail\Mail();
    $email->setFrom("test@example.com", "Example User");
    $email->setSubject("Sending with Twilio SendGrid is Fun");
    $email->addTo("test@example.com", "Example User");
    
    $email->addContent(
        "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
    );
    
    
    $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
    try {
        $response = $sendgrid->send($email);
        print $response->statusCode() . "\n";
        print_r($response->headers());
        print $response->body() . "\n";
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."\n";
    }
    
    
    

    4. Using template blade

    modify the above code as given below

    
    $content = view('emails.paymentSuccess', $email_content)->render();
    
    $email->addContent("text/html", $content);